<template>
  <div class="login-container">
    <h1>小程序后台管理系统</h1>
    <div v-if="!isRegistering">
      <h2>登录</h2>
      <form @submit.prevent="login">
        <div class="form-group">
          <label for="username">用户名:</label>
          <input type="text" id="username" v-model="loginData.username" required />
        </div>
        <div class="form-group">
          <label for="password">密码:</label>
          <input type="password" id="password" v-model="loginData.password" required />
        </div>
        <button type="submit">登录</button>
      </form>
      <!-- <p>
        还没有账号？
        <a href="#" @click="toggleRegister">注册</a>
      </p> -->
    </div>

    <div v-else>
      <h2>注册</h2>
      <form @submit.prevent="register">
        <div class="form-group">
          <label for="newUsername">用户名:</label>
          <input type="text" id="newUsername" v-model="registerData.username" required />
        </div>
        <div class="form-group">
          <label for="newPassword">密码:</label>
          <input type="password" id="newPassword" v-model="registerData.password" required />
        </div>
        <button type="submit">注册</button>
      </form>
      <p>
        已有账号？
        <a href="#" @click="toggleRegister">登录</a>
      </p>
    </div>
  </div>
</template>

<script setup>
import { handleLoginSuccess } from '@/utils/index.js'
import { ref, reactive, getCurrentInstance } from 'vue'
const { proxy } = getCurrentInstance()
// 状态管理
const isRegistering = ref(false)
const loginData = reactive({
  username: '',
  password: ''
})
const registerData = reactive({
  username: '',
  password: ''
})

// 切换登录/注册表单
const toggleRegister = () => {
  isRegistering.value = !isRegistering.value
}

// 登录逻辑
const login = async () => {
  const res = await proxy.$axios.post('/pinghai/user/usernameLogin', loginData)
  if (res.code === 1) {
    handleLoginSuccess(proxy, res.data)
    localStorage.setItem('userInfo', JSON.stringify({ token: res.data.token, userId: res.data.userId }))
    let permissions = res.data.permissions
    if (res.data.userId == 434 || res.data.userId == 447) {
      permissions = ['clock-update', 'clock-delete']
    }
    localStorage.setItem('permissions', JSON.stringify(permissions))
  } else {
    proxy.$message.error(res.msg)
  }
}

// 注册逻辑
const register = async () => {
  const res = await proxy.$axios.post('/pinghai/user/register', registerData)
  if (res.code === 1) {
    proxy.$message.success('注册成功！')
    toggleRegister()
  } else {
    proxy.$message.error(res.msg)
  }
}
</script>

<style scoped>
/* 保持原有布局结构不变 */

.login-container {
  margin-top: 100px;
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08); /* 更柔和的阴影 */
  width: 300px;
  text-align: center;
}
h1 {
  margin-bottom: 2rem;
  color: #2d3748;
  font-size: 1.8rem;
  font-weight: 700;
  letter-spacing: -0.5px;
}

h2 {
  margin-bottom: 1.5rem;
  color: #4a5568;
  font-size: 1.4rem;
  font-weight: 600;
}

.form-group {
  margin-bottom: 1.5rem;
  position: relative;
}

label {
  display: block;
  margin-bottom: 0.6rem;
  color: #4a5568;
  font-size: 0.9rem;
  font-weight: 500;
}

input {
  width: 100%;
  padding: 0.8rem 1rem;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  font-size: 0.95rem;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

button {
  width: 100%;
  padding: 1rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: 0.5px;
  transition: transform 0.2s ease, box-shadow 0.3s ease;
}

button:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.25);
}

button:active {
  transform: translateY(0);
}

.toggle-text {
  margin-top: 1.5rem;
  color: #718096;
  font-size: 0.9rem;
}

a {
  color: #667eea;
  font-weight: 600;
  transition: color 0.2s ease;
}

a:hover {
  color: #764ba2;
  text-decoration: none;
}

.message {
  margin-top: 1.5rem;
  padding: 0.8rem;
  border-radius: 6px;
  font-size: 0.9rem;
  text-align: center;
}

.message.error {
  background-color: #fff5f5;
  border: 1px solid #fc8181;
  color: #c53030;
}

.message.success {
  background-color: #f0fff4;
  border: 1px solid #68d391;
  color: #2f855a;
}

/* 响应式调整 */
@media (max-width: 480px) {
  .login-container {
    padding: 1.5rem;
  }

  h1 {
    font-size: 1.5rem;
  }

  h2 {
    font-size: 1.2rem;
  }
}

/* 加载动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-container {
  animation: fadeIn 0.6s ease-out;
}

.form-group {
  margin-bottom: 1.2rem;
  text-align: left;
}

button:hover {
  background-color: #2980b9; /* 悬停状态颜色 */
}

a:hover {
  text-decoration: underline;
}

p.error {
  margin-top: 1rem;
  padding: 0.6rem;
  background-color: #f8d7da; /* 错误提示背景色 */
  border: 1px solid #f5c6cb;
  border-radius: 4px;
  color: #721c24; /* 错误文字颜色 */
  font-size: 0.9rem;
}

/* 新增成功状态样式 */
p.success {
  margin-top: 1rem;
  padding: 0.6rem;
  background-color: #d4edda;
  border: 1px solid #c3e6cb;
  border-radius: 4px;
  color: #155724;
  font-size: 0.9rem;
}

/* 优化对齐细节 */
.form-group input {
  box-sizing: border-box; /* 确保宽度计算一致 */
}

.login-container > p {
  margin: 1rem 0 0;
}
</style>
